package com.icefish.control;

import com.alibaba.fastjson.JSONObject;
import com.icefish.bean.Comment;
import com.icefish.bean.Order;
import com.icefish.bean.fastjsonParse.Meta;
import com.icefish.dao.CommentDao;
import com.icefish.dao.OrderDao;
import com.icefish.daoFactory.DaoFactory;
import com.icefish.utils.Init_Req_Resp_Config;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet("/updateOrderSays")
public class UpdateOrderSays extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Init_Req_Resp_Config.init(request, response);

        JSONObject jsonObject = new JSONObject();
        Meta meta = new Meta();

        // 1、捞取 order_id和 says  捞取comment相关数据 goods_id, user_id, username (says_time自己生成)
        String order_id = request.getParameter("order_id");
        String says = request.getParameter("says");
        String goods_id = request.getParameter("goods_id");
        String user_id = request.getParameter("user_id");
        String username = request.getParameter("username");

        // 不管有没有用到，提前制作一些类
        // 订单bean
        Order order = new Order();
        order.setOrder_id(Integer.parseInt(order_id));
        order.setSays(says);

        // 评论bean
        Comment comment = new Comment();
        comment.setSays_time(new Date());
        comment.setSays(says);
        comment.setGoods_id(Integer.parseInt(goods_id));
        comment.setUser_id(Integer.parseInt(user_id));
        comment.setUsername(username);
        comment.setOrder_id(Integer.parseInt(order_id));


        // 2、修改order表的评论数据
        OrderDao orderdao = DaoFactory.getOrderDaoInstance();
        boolean updateOrderSaysFlag = false;
        try {
            updateOrderSaysFlag = orderdao.updateOrderSays(order);
        }catch (Exception e) {
            e.printStackTrace();
        }

//        修改order数据成功，继续操作comment表
        if (updateOrderSaysFlag) {
            // 3、用order_id查询comment表是否有数据
            CommentDao commentDao = DaoFactory.getCommentDaoInstance();
            boolean hasCommentDataFlag = false;
            try {
                hasCommentDataFlag = commentDao.selectHasCommentByOrderId(order.getOrder_id());
            }catch (Exception e) {
                e.printStackTrace();
            }


            // 3.1、没有数据为comment表新增一条数据
            if (!hasCommentDataFlag) {
                try {
                    boolean addStatus = commentDao.addComment(comment);
                    if (addStatus) {
                        meta.setMsg("修改order表says成功，新增comment表says成功");
                        meta.setStatus(200);
                    } else {
                        meta.setMsg("修改order表says成功，新增comment表says失败");
                        meta.setStatus(500);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            // 3.2、有数据，进行update操作
            if (hasCommentDataFlag) {
                try {
                    boolean updateStatus = commentDao.updateSaysByOrderId(comment.getOrder_id(),comment.getSays());
                    if (updateStatus) {
                        meta.setMsg("修改order表says成功，修改comment表says成功");
                        meta.setStatus(200);
                    } else {
                        meta.setMsg("修改order表says成功，修改comment表says失败");
                        meta.setStatus(500);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
//        第一步 修改订单评论就失败的响应
        else {
            meta.setStatus(500);
            meta.setMsg("修改评论数据失败——order表Update");
        }

        // 响应数据
        String backToBrowser = jsonObject.toJSONString(meta);
        // 响应数据
        response.getWriter().write(backToBrowser);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
